package com.dure.hotmemory.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.dure.hotmemory.R;
import com.dure.hotmemory.bean.Code;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by leix on 30/05/2017.
 */

public class CodeGridAdapter extends BaseAdapter {

    List<Code> codeList = new ArrayList<Code>();
    Context context;

    public CodeGridAdapter(Context context){
        this.context = context;
    }

    @Override
    public int getCount() {
        return codeList.size();
    }

    @Override
    public Code getItem(int i) {
        return codeList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null){
            view = LayoutInflater.from(context).inflate(R.layout.code_item, null);
            holder = new ViewHolder();
            holder.nameTV = (TextView) view.findViewById(R.id.name_tv);
            holder.numberTV = (TextView) view.findViewById(R.id.number_tv);
            view.setTag(holder);
        }else{
            holder = (ViewHolder) view.getTag();
        }

        Code code = getItem(i);

        holder.nameTV.setText(code.name);
        holder.numberTV.setText(code.number);
        return view;
    }

    public void setDataList(List<Code> codeList){
        if (codeList != null && codeList.size() > 0){
            this.codeList.clear();
            this.codeList.addAll(codeList);
            notifyDataSetChanged();
        }
    }

    class ViewHolder{
        TextView numberTV, nameTV;
    }
}
